import { _decorator, Component, Game, game, Node, ProgressBar, Sprite, Tween, tween, UIOpacity, UITransform, v3, Widget } from 'cc';
import $planet_GlobalDC_poof$ from '../../../frame/common/data.global.center';
import $planet_DC_poof$ from '../data/data.center';
import { $planet_AudioManager_poof$ } from '../manager/audio-manager';
import $planet_Config_poof$ from '../manager/config.dynamic';
import $planet_GameAssets_poof$ from '../manager/game-assets';
import { $planet_ClientAPI_poof$ } from '../api/client.api';
const { ccclass, property } = _decorator;

@ccclass('$planet_Loading_poof$')
export class $planet_Loading_poof$ extends Component {

	@property(UITransform)
	private $planet_ui_background_poof$: UITransform = null;
	@property(ProgressBar)
	private $planet_bar_poof$: ProgressBar = null;

	private $planet_loading_duration_poof$ = Infinity;
	private $planet_loading_total_poof$ = 0;
	private $planet_loaded_poof$ = false;
	private $planet_progress_handle_poof$: Node;

    start() {
		this.$planet_progress_handle_poof$ = this.$planet_bar_poof$.barSprite.node.getChildByName('handle');

		this.$planet_loaded_poof$ = false;
		this.$planet_adaptUI_poof$();

		game.on(Game.EVENT_ENGINE_INITED, ()=>{
			$planet_GlobalDC_poof$.$planet_init_poof$();

			$planet_ClientAPI_poof$.$planet_Local_poof$()
			.then(res=>$planet_Config_poof$.$planet_init_poof$(res.$planet_countryCode_poof$, res.$planet_languageCode_poof$))
			.then(()=>$planet_DC_poof$.$planet_init_poof$())
			.then(()=>$planet_GameAssets_poof$.$planet_init_poof$())
			.then(()=>$planet_DC_poof$.$planet_synchUserInfo_poof$())
			.then(()=>this.$planet_run_poof$());
		});
    }

    update(deltaTime: number) {
		if(this.$planet_loaded_poof$) return void 0;
		this.$planet_loading_total_poof$ += deltaTime;

		let progress = abd.$planet_number_poof$.$planet_limit_poof$(this.$planet_loading_total_poof$ / this.$planet_loading_duration_poof$, 0, 1);
		this.$planet_setProgress_poof$(progress);
		if(progress >= 1){
			this.$planet_loaded_poof$ = true;
			this.$planet_enterGame_poof$();
		}
    }

	private $planet_setProgress_poof$(progress: number){
		progress = Math.min(1, Math.max(0, progress));
		this.$planet_bar_poof$.progress = progress;
		let {x, y, z} = this.$planet_progress_handle_poof$.position;
		x = progress * this.$planet_bar_poof$.barSprite.getComponent(UITransform).width;
		this.$planet_progress_handle_poof$.setPosition(x, y, z);
	}

	private $planet_adaptUI_poof$(){
		let size = this.$planet_ui_background_poof$.getComponent(Sprite).spriteFrame.originalSize;
		let ratio = abd.$planet_size_poof$.$planet_exterRatio_poof$(size, abd.$planet_size_poof$.$planet_canvasSize_poof$);
		this.$planet_ui_background_poof$.setContentSize(size.width * ratio, size.height * ratio);

		tween(this.$planet_progress_handle_poof$)
		.by(1, {angle: -360}).repeatForever().start();
	}

	private $planet_run_poof$(duration?: number){
		this.$planet_loading_duration_poof$ = duration??6;
		this.$planet_loading_total_poof$ = 0;
		this.node.parent.children.forEach(n=>n.active = true);
	}

	private $planet_enterGame_poof$(){
		this.$planet_leave_poof$();
	}

	private $planet_leave_poof$(){

		let np = new abd.$planet_NudityPromise_poof$<Node>();

		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.enabled = false;

		let ui_opacity = this.node.getComponent(UIOpacity);

		Tween.stopAllByTarget(this.$planet_progress_handle_poof$);

		tween(ui_opacity).to(0.5, {opacity: 0}, {easing: 'quartInOut'}).call(()=>np.$planet_resolve_poof$(this.node)).start();
		tween(this.node).to(0.5, {scale: v3(1.1, 1.1, 0)}, {easing: 'quartInOut'}).start();

		np.$planet_promise_poof$.then(node=>{
			node.removeFromParent();
			node.destroy();
			$planet_DC_poof$.$planet_trigger_poof$.$planet_UI_HOME_poof$ = true;
			$planet_AudioManager_poof$.$planet_ins_poof$.$planet_playBGM_poof$();
		});

		return np.$planet_promise_poof$;
	}
}

